package org.geek4j.base.service.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.geek4j.base.persistence.repository.BaseRepository;
import org.geek4j.base.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author Geek4J
 * @since 2016年4月9日 上午10:57:19
 * @description BaseServiceImpl CRUD
 */
public class BaseServiceImpl<T, ID extends Serializable> implements BaseService<T, ID> {

	@Autowired
	private BaseRepository<T, ID> baseRepository;

	@Transactional
	public T save(T entity) {
		return baseRepository.save(entity);
	}

	@Transactional
	public List<T> save(List<T> entities) {
		return baseRepository.save(entities);
	}

	public T findOne(ID id) {
		return baseRepository.findOne(id);
	}

	public boolean exists(ID id) {
		return baseRepository.exists(id);
	}

	public List<T> findAll() {
		return baseRepository.findAll();
	}

	public List<T> findAll(List<ID> ids) {
		List<ID> idsList = new ArrayList<ID>();
		for (ID id : ids) {
			idsList.add(id);
		}
		return baseRepository.findAll(idsList);
	}

	public List<T> findAll(Sort sort) {
		return baseRepository.findAll(sort);
	}

	public Page<T> findAll(Pageable pageable) {
		return baseRepository.findAll(pageable);
	}

	public long count() {
		return baseRepository.count();
	}

	@Transactional
	public void delete(ID id) {
		baseRepository.delete(id);
	}

	@Transactional
	public void delete(T entity) {
		baseRepository.delete(entity);
	}

	@Transactional
	public void deleteByIds(List<ID> ids) {
		for (ID id : ids) {
			baseRepository.delete(id);
		}
	}

	@Transactional
	public void delete(List<? extends T> entities) {
		baseRepository.delete(entities);
	}
}
